package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Hospital;
import com.itheima.service.HospitalService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/hospital")
public class HospitalController {

    @Reference
    private HospitalService hospitalService;


    //查找全部
    @RequestMapping("/findAllHospital")
    public Result findAll(){

        List<Hospital> hospitalList =hospitalService.findAll();
        List<Map<String,Object>> mapList = new ArrayList<>();

        for (Hospital hospital : hospitalList) {
            Map<String,Object> map = new HashMap<>();
            map.put("name",hospital.getName());
            map.put("id",hospital.getId());
            mapList.add(map);
        }
        if (hospitalList!=null&&hospitalList.size()>0){
            //查询成功
            return new Result(true,MessageConstant.QUERY_HOSPITAL_SUCCESS,mapList);
        }else{
            //查询失败
            return new Result(false,MessageConstant.QUERY_HOSPITAL_FAIL);
        }
    }

    //编辑医院信息
    @RequestMapping("/edit")
    public Result edit(@RequestBody Hospital hospital){
        try {
            hospitalService.edit(hospital);
            return new Result(true,MessageConstant.EDIT_HOSPITAL_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_HOSPITAL_FAIL);
        }
    }

    //编辑医院信息
    @RequestMapping("/findAllAddr")
    public Result findAllAddr(){
        try {
            List<Map<String,Object>> list = hospitalService.findAllAddr();
            return new Result(true,MessageConstant.EDIT_HOSPITAL_SUCCESS,list);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_HOSPITAL_FAIL);
        }
    }

}
